package com.bdqn.service;

import com.bdqn.mapper.SupplierMapper;
import com.bdqn.pojo.Supplier;
import com.bdqn.service.SupplierService;
import com.bdqn.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

import java.util.List;
/**
 * 供货商功能模块业务实现类
 */
public class SupplierServiceImpl implements SupplierService {
    private Logger logger = LogManager.getLogger(SupplierServiceImpl.class);

    /**
     * 分页查询供货商列表
     * @param pageNo
     * @param pageSize
     * @param querySupCode
     * @param querySupName
     * @return
     */
    public List<Supplier> selectPageList(Integer pageNo,Integer pageSize,String querySupCode,String querySupName){
        if(pageNo == null || pageSize == null){
            return null;
        }
        SqlSession sqlSession = null;
        List<Supplier> list = null;
        try{
            sqlSession = MyBatisUtil.createSqlSession();
            list = sqlSession.getMapper(SupplierMapper.class).selectPageList(pageNo,pageSize,querySupCode,querySupName);
        }catch (Exception e){
            logger.error(e.getMessage());
        } finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        return list;
    }

    /**
     * 查询总记录数
     * @param querySupCode
     * @param querySupName
     * @return
     */
    @Override
    public int count(String querySupCode,String querySupName){
        SqlSession sqlSession = null;
        int count = 0;
        try{
            sqlSession = MyBatisUtil.createSqlSession();
            count = sqlSession.getMapper(SupplierMapper.class).count(querySupCode,querySupName);
        }catch (Exception e){
            logger.error(e.getMessage());
        } finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        return count;
    }
    /**
     * 查询所有供货商的id和名称
     * @return
     */
    @Override
    public List<Supplier> list(){
        SqlSession sqlSession = null;
        List<Supplier> list = null;
        try{
            sqlSession = MyBatisUtil.createSqlSession();
            list = sqlSession.getMapper(SupplierMapper.class).list();
        }catch (Exception e){
            logger.error(e.getMessage());
        } finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        return list;
    }

    /**
     * 新增供货商信息
     *
     * @param supplier
     * @return
     */
    @Override
    public int add(Supplier supplier){
        if(supplier == null){
            return 0;
        }
        SqlSession sqlSession = null;
        int count = 0;
        try{
            sqlSession = MyBatisUtil.createSqlSession();
            count = sqlSession.getMapper(SupplierMapper.class).add(supplier);
            sqlSession.commit();
        }catch (Exception e){
            logger.error(e.getMessage());
            sqlSession.rollback();
        } finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        return count;
    }

    /**
     * 根据ID查询供货商
     *
     * @param id
     * @return
     */
    @Override
    public Supplier getId(Integer id) {
        if(id == null){
            return null;
        }
        SqlSession sqlSession = null;
        Supplier supplier = null;
        try{
            sqlSession = MyBatisUtil.createSqlSession();
            supplier = sqlSession.getMapper(SupplierMapper.class).getId(id);
        }catch (Exception e){
            logger.error(e.getMessage());
        } finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        return supplier;
    }

    /**
     * 根据ID进行删除
     *
     * @param id
     * @return
     */
    @Override
    public int delete(Integer id) {
        if(id == null){
            return 0;
        }
        SqlSession sqlSession = null;
        int count = 0;
        try{
            sqlSession = MyBatisUtil.createSqlSession();
            count = sqlSession.getMapper(SupplierMapper.class).delete(id);
            sqlSession.commit();
        }catch (Exception e){
            logger.error(e.getMessage());
            sqlSession.rollback();
        } finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        return count;
    }

    /**
     * 根据ID进行修改
     *
     * @param supplier
     * @return
     */
    @Override
    public int update(Supplier supplier) {
        if(supplier == null){
            return 0;
        }
        SqlSession sqlSession = null;
        int count = 0;
        try{
            sqlSession = MyBatisUtil.createSqlSession();
            count = sqlSession.getMapper(SupplierMapper.class).update(supplier);
            sqlSession.commit();
        }catch (Exception e){
            logger.error(e.getMessage());
            sqlSession.rollback();
        } finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        return count;
    }
}
